<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>最新提案 | Evan&#39;s blog</title>
    <meta name="generator" content="VuePress 1.9.5">
    <link rel="icon" href="/blog/img/favicon.ico">
    <meta name="description" content="web前端技术博客,专注web前端学习与总结。JavaScript,js,ES6,TypeScript,vue,React,python,css3,html5,Node,git,github等技术文章。">
    <meta name="keywords" content="前端博客,个人技术博客,前端,前端开发,前端框架,web前端,前端面试题,技术文档,学习,面试,JavaScript,js,ES6,TypeScript,vue,python,css3,html5,Node,git,github,markdown">
    <meta name="baidu-site-verification" content="7F55weZDDc">
    <meta name="theme-color" content="#11a8cd">
    
    <link rel="preload" href="/blog/assets/css/0.styles.5cae76fb.css" as="style"><link rel="preload" href="/blog/assets/js/app.aaf1b95a.js" as="script"><link rel="preload" href="/blog/assets/js/2.4e88da26.js" as="script"><link rel="preload" href="/blog/assets/js/112.9ecfc68a.js" as="script"><link rel="prefetch" href="/blog/assets/js/10.77cd5446.js"><link rel="prefetch" href="/blog/assets/js/100.a4b7c22a.js"><link rel="prefetch" href="/blog/assets/js/101.f10a486b.js"><link rel="prefetch" href="/blog/assets/js/102.c6fb27ad.js"><link rel="prefetch" href="/blog/assets/js/103.6544c57a.js"><link rel="prefetch" href="/blog/assets/js/104.6da84eb0.js"><link rel="prefetch" href="/blog/assets/js/105.e98afb5b.js"><link rel="prefetch" href="/blog/assets/js/106.00c6a618.js"><link rel="prefetch" href="/blog/assets/js/107.3e384ab5.js"><link rel="prefetch" href="/blog/assets/js/108.cc8462f3.js"><link rel="prefetch" href="/blog/assets/js/109.efac7118.js"><link rel="prefetch" href="/blog/assets/js/11.0b77905f.js"><link rel="prefetch" href="/blog/assets/js/110.bfd96a59.js"><link rel="prefetch" href="/blog/assets/js/111.d67fb86c.js"><link rel="prefetch" href="/blog/assets/js/113.dd57f9de.js"><link rel="prefetch" href="/blog/assets/js/114.a12ac3b9.js"><link rel="prefetch" href="/blog/assets/js/115.856e382b.js"><link rel="prefetch" href="/blog/assets/js/116.941afac4.js"><link rel="prefetch" href="/blog/assets/js/117.df53da43.js"><link rel="prefetch" href="/blog/assets/js/118.906f3d41.js"><link rel="prefetch" href="/blog/assets/js/119.33bd4cde.js"><link rel="prefetch" href="/blog/assets/js/12.6a1ae269.js"><link rel="prefetch" href="/blog/assets/js/120.c7d90a2a.js"><link rel="prefetch" href="/blog/assets/js/121.e9a583b9.js"><link rel="prefetch" href="/blog/assets/js/122.d2edbcb6.js"><link rel="prefetch" href="/blog/assets/js/123.1a632657.js"><link rel="prefetch" href="/blog/assets/js/124.2a2decb0.js"><link rel="prefetch" href="/blog/assets/js/125.f9344214.js"><link rel="prefetch" href="/blog/assets/js/126.90a937ca.js"><link rel="prefetch" href="/blog/assets/js/127.541a2bd8.js"><link rel="prefetch" href="/blog/assets/js/128.d197e7a9.js"><link rel="prefetch" href="/blog/assets/js/129.bd2b74bf.js"><link rel="prefetch" href="/blog/assets/js/13.a3d3aa42.js"><link rel="prefetch" href="/blog/assets/js/130.4555d2a9.js"><link rel="prefetch" href="/blog/assets/js/131.af4d4381.js"><link rel="prefetch" href="/blog/assets/js/132.15b408e6.js"><link rel="prefetch" href="/blog/assets/js/133.c6819d56.js"><link rel="prefetch" href="/blog/assets/js/134.2b49eb48.js"><link rel="prefetch" href="/blog/assets/js/135.7b5224d1.js"><link rel="prefetch" href="/blog/assets/js/136.e966c07b.js"><link rel="prefetch" href="/blog/assets/js/137.962a7287.js"><link rel="prefetch" href="/blog/assets/js/138.e4446fa5.js"><link rel="prefetch" href="/blog/assets/js/139.d0a5415a.js"><link rel="prefetch" href="/blog/assets/js/14.d68bef9a.js"><link rel="prefetch" href="/blog/assets/js/140.23d79a44.js"><link rel="prefetch" href="/blog/assets/js/141.e09939d0.js"><link rel="prefetch" href="/blog/assets/js/142.266a27e9.js"><link rel="prefetch" href="/blog/assets/js/143.ed3c54b1.js"><link rel="prefetch" href="/blog/assets/js/144.98c6792d.js"><link rel="prefetch" href="/blog/assets/js/145.a18f3f3f.js"><link rel="prefetch" href="/blog/assets/js/146.106bb541.js"><link rel="prefetch" href="/blog/assets/js/147.4b313651.js"><link rel="prefetch" href="/blog/assets/js/148.4a6ec42d.js"><link rel="prefetch" href="/blog/assets/js/149.9ec297ab.js"><link rel="prefetch" href="/blog/assets/js/15.81a99c55.js"><link rel="prefetch" href="/blog/assets/js/150.823ccad7.js"><link rel="prefetch" href="/blog/assets/js/151.1eee0fd4.js"><link rel="prefetch" href="/blog/assets/js/152.71ae0047.js"><link rel="prefetch" href="/blog/assets/js/153.63d5a471.js"><link rel="prefetch" href="/blog/assets/js/154.77169175.js"><link rel="prefetch" href="/blog/assets/js/155.c460c129.js"><link rel="prefetch" href="/blog/assets/js/156.793e4b57.js"><link rel="prefetch" href="/blog/assets/js/157.c9261336.js"><link rel="prefetch" href="/blog/assets/js/158.f2160023.js"><link rel="prefetch" href="/blog/assets/js/159.33ddd8b3.js"><link rel="prefetch" href="/blog/assets/js/16.6a1f4400.js"><link rel="prefetch" href="/blog/assets/js/160.cd0b03b9.js"><link rel="prefetch" href="/blog/assets/js/161.d4b3bb73.js"><link rel="prefetch" href="/blog/assets/js/162.c69e83b1.js"><link rel="prefetch" href="/blog/assets/js/163.9c1b18fa.js"><link rel="prefetch" href="/blog/assets/js/164.854148c8.js"><link rel="prefetch" href="/blog/assets/js/165.aa0c4f4d.js"><link rel="prefetch" href="/blog/assets/js/166.54fc4b36.js"><link rel="prefetch" href="/blog/assets/js/167.ca2d6801.js"><link rel="prefetch" href="/blog/assets/js/168.cf378109.js"><link rel="prefetch" href="/blog/assets/js/169.a298949f.js"><link rel="prefetch" href="/blog/assets/js/17.ad83a175.js"><link rel="prefetch" href="/blog/assets/js/170.1f5f6b47.js"><link rel="prefetch" href="/blog/assets/js/171.aec76c3b.js"><link rel="prefetch" href="/blog/assets/js/172.be4a4852.js"><link rel="prefetch" href="/blog/assets/js/173.80a0f3b5.js"><link rel="prefetch" href="/blog/assets/js/174.e3a39617.js"><link rel="prefetch" href="/blog/assets/js/175.597dc79e.js"><link rel="prefetch" href="/blog/assets/js/176.29eb04a2.js"><link rel="prefetch" href="/blog/assets/js/177.e91f6f26.js"><link rel="prefetch" href="/blog/assets/js/178.34ce2871.js"><link rel="prefetch" href="/blog/assets/js/179.f2cef142.js"><link rel="prefetch" href="/blog/assets/js/18.11cfb1b5.js"><link rel="prefetch" href="/blog/assets/js/180.327428c1.js"><link rel="prefetch" href="/blog/assets/js/181.d6f70081.js"><link rel="prefetch" href="/blog/assets/js/182.e5313823.js"><link rel="prefetch" href="/blog/assets/js/183.d08dbc32.js"><link rel="prefetch" href="/blog/assets/js/184.ccfa8d5d.js"><link rel="prefetch" href="/blog/assets/js/185.9695e392.js"><link rel="prefetch" href="/blog/assets/js/186.3a1b272d.js"><link rel="prefetch" href="/blog/assets/js/187.e11331a6.js"><link rel="prefetch" href="/blog/assets/js/188.2a3eab47.js"><link rel="prefetch" href="/blog/assets/js/189.71a6890e.js"><link rel="prefetch" href="/blog/assets/js/19.1cca0d39.js"><link rel="prefetch" href="/blog/assets/js/190.41f05291.js"><link rel="prefetch" href="/blog/assets/js/191.6db1a29f.js"><link rel="prefetch" href="/blog/assets/js/192.6d6a9425.js"><link rel="prefetch" href="/blog/assets/js/193.1ac4e1e9.js"><link rel="prefetch" href="/blog/assets/js/194.e3056d02.js"><link rel="prefetch" href="/blog/assets/js/195.937d6e29.js"><link rel="prefetch" href="/blog/assets/js/196.775b3da3.js"><link rel="prefetch" href="/blog/assets/js/197.437fe16f.js"><link rel="prefetch" href="/blog/assets/js/198.e02c3fd3.js"><link rel="prefetch" href="/blog/assets/js/199.da036572.js"><link rel="prefetch" href="/blog/assets/js/20.f58b2515.js"><link rel="prefetch" href="/blog/assets/js/200.d70ebfc1.js"><link rel="prefetch" href="/blog/assets/js/201.5a0cc6a8.js"><link rel="prefetch" href="/blog/assets/js/202.b6f4dfe9.js"><link rel="prefetch" href="/blog/assets/js/203.ac5f2eea.js"><link rel="prefetch" href="/blog/assets/js/204.e4197e35.js"><link rel="prefetch" href="/blog/assets/js/205.e988b5e5.js"><link rel="prefetch" href="/blog/assets/js/206.7d6d7ee2.js"><link rel="prefetch" href="/blog/assets/js/207.e9476276.js"><link rel="prefetch" href="/blog/assets/js/208.90d387ec.js"><link rel="prefetch" href="/blog/assets/js/209.01278130.js"><link rel="prefetch" href="/blog/assets/js/21.f2eb7cbe.js"><link rel="prefetch" href="/blog/assets/js/210.08914606.js"><link rel="prefetch" href="/blog/assets/js/211.b5ae3862.js"><link rel="prefetch" href="/blog/assets/js/212.03a9909c.js"><link rel="prefetch" href="/blog/assets/js/213.88999a25.js"><link rel="prefetch" href="/blog/assets/js/214.5e6517ae.js"><link rel="prefetch" href="/blog/assets/js/215.7c05faa3.js"><link rel="prefetch" href="/blog/assets/js/216.0c0d0250.js"><link rel="prefetch" href="/blog/assets/js/217.3e6a1882.js"><link rel="prefetch" href="/blog/assets/js/218.7bdaa47b.js"><link rel="prefetch" href="/blog/assets/js/219.4a9b382a.js"><link rel="prefetch" href="/blog/assets/js/22.0225f4f3.js"><link rel="prefetch" href="/blog/assets/js/220.da13afe0.js"><link rel="prefetch" href="/blog/assets/js/221.b93baddd.js"><link rel="prefetch" href="/blog/assets/js/222.6f88fa87.js"><link rel="prefetch" href="/blog/assets/js/223.23532ff7.js"><link rel="prefetch" href="/blog/assets/js/224.bba40504.js"><link rel="prefetch" href="/blog/assets/js/225.e87911f0.js"><link rel="prefetch" href="/blog/assets/js/226.27fcd8f6.js"><link rel="prefetch" href="/blog/assets/js/227.7075a8c3.js"><link rel="prefetch" href="/blog/assets/js/228.d5363587.js"><link rel="prefetch" href="/blog/assets/js/229.95d21664.js"><link rel="prefetch" href="/blog/assets/js/23.203b3a9e.js"><link rel="prefetch" href="/blog/assets/js/230.eee47d34.js"><link rel="prefetch" href="/blog/assets/js/231.686012e1.js"><link rel="prefetch" href="/blog/assets/js/232.d780f01b.js"><link rel="prefetch" href="/blog/assets/js/233.fba323db.js"><link rel="prefetch" href="/blog/assets/js/234.7ac11688.js"><link rel="prefetch" href="/blog/assets/js/235.61ba2d32.js"><link rel="prefetch" href="/blog/assets/js/24.26ad942d.js"><link rel="prefetch" href="/blog/assets/js/25.e9864d4c.js"><link rel="prefetch" href="/blog/assets/js/26.1a4e2ed9.js"><link rel="prefetch" href="/blog/assets/js/27.f56ff53e.js"><link rel="prefetch" href="/blog/assets/js/28.3c5bd15e.js"><link rel="prefetch" href="/blog/assets/js/29.0104f169.js"><link rel="prefetch" href="/blog/assets/js/3.3ec6f00b.js"><link rel="prefetch" href="/blog/assets/js/30.9aaf650a.js"><link rel="prefetch" href="/blog/assets/js/31.50bfe700.js"><link rel="prefetch" href="/blog/assets/js/32.33630021.js"><link rel="prefetch" href="/blog/assets/js/33.703693c2.js"><link rel="prefetch" href="/blog/assets/js/34.51cfd584.js"><link rel="prefetch" href="/blog/assets/js/35.5614a593.js"><link rel="prefetch" href="/blog/assets/js/36.3f9fb418.js"><link rel="prefetch" href="/blog/assets/js/37.d3471a6e.js"><link rel="prefetch" href="/blog/assets/js/38.303beb9a.js"><link rel="prefetch" href="/blog/assets/js/39.528e5c8c.js"><link rel="prefetch" href="/blog/assets/js/4.f7a68e82.js"><link rel="prefetch" href="/blog/assets/js/40.5d37d2ac.js"><link rel="prefetch" href="/blog/assets/js/41.5e434fb6.js"><link rel="prefetch" href="/blog/assets/js/42.58ebb1e1.js"><link rel="prefetch" href="/blog/assets/js/43.f7344b04.js"><link rel="prefetch" href="/blog/assets/js/44.ba8e2c32.js"><link rel="prefetch" href="/blog/assets/js/45.b59d07a4.js"><link rel="prefetch" href="/blog/assets/js/46.cf56a467.js"><link rel="prefetch" href="/blog/assets/js/47.2307b39c.js"><link rel="prefetch" href="/blog/assets/js/48.0f967628.js"><link rel="prefetch" href="/blog/assets/js/49.e9c76177.js"><link rel="prefetch" href="/blog/assets/js/5.43eb1773.js"><link rel="prefetch" href="/blog/assets/js/50.ef1c92fd.js"><link rel="prefetch" href="/blog/assets/js/51.472db098.js"><link rel="prefetch" href="/blog/assets/js/52.b151aea0.js"><link rel="prefetch" href="/blog/assets/js/53.0df4c526.js"><link rel="prefetch" href="/blog/assets/js/54.352990e8.js"><link rel="prefetch" href="/blog/assets/js/55.27accd61.js"><link rel="prefetch" href="/blog/assets/js/56.91d392d0.js"><link rel="prefetch" href="/blog/assets/js/57.4f8d55c3.js"><link rel="prefetch" href="/blog/assets/js/58.aba5c165.js"><link rel="prefetch" href="/blog/assets/js/59.1f31aefa.js"><link rel="prefetch" href="/blog/assets/js/6.709e31d8.js"><link rel="prefetch" href="/blog/assets/js/60.c4c2ad7c.js"><link rel="prefetch" href="/blog/assets/js/61.84c44795.js"><link rel="prefetch" href="/blog/assets/js/62.847d5a79.js"><link rel="prefetch" href="/blog/assets/js/63.4526d774.js"><link rel="prefetch" href="/blog/assets/js/64.149c457f.js"><link rel="prefetch" href="/blog/assets/js/65.ffb7241e.js"><link rel="prefetch" href="/blog/assets/js/66.ab3a1fd7.js"><link rel="prefetch" href="/blog/assets/js/67.960d17a4.js"><link rel="prefetch" href="/blog/assets/js/68.1ecea3e5.js"><link rel="prefetch" href="/blog/assets/js/69.24a059f9.js"><link rel="prefetch" href="/blog/assets/js/7.3123b0d0.js"><link rel="prefetch" href="/blog/assets/js/70.087b119f.js"><link rel="prefetch" href="/blog/assets/js/71.36ec64ad.js"><link rel="prefetch" href="/blog/assets/js/72.72180002.js"><link rel="prefetch" href="/blog/assets/js/73.76a3edbc.js"><link rel="prefetch" href="/blog/assets/js/74.fb379a54.js"><link rel="prefetch" href="/blog/assets/js/75.eaa559bd.js"><link rel="prefetch" href="/blog/assets/js/76.156bd9c3.js"><link rel="prefetch" href="/blog/assets/js/77.123a161c.js"><link rel="prefetch" href="/blog/assets/js/78.19a324be.js"><link rel="prefetch" href="/blog/assets/js/79.e24877d1.js"><link rel="prefetch" href="/blog/assets/js/8.5c81f7fc.js"><link rel="prefetch" href="/blog/assets/js/80.22910223.js"><link rel="prefetch" href="/blog/assets/js/81.169a78ab.js"><link rel="prefetch" href="/blog/assets/js/82.c300aae5.js"><link rel="prefetch" href="/blog/assets/js/83.fc555e72.js"><link rel="prefetch" href="/blog/assets/js/84.8e8a9c99.js"><link rel="prefetch" href="/blog/assets/js/85.7c060e20.js"><link rel="prefetch" href="/blog/assets/js/86.946a80b1.js"><link rel="prefetch" href="/blog/assets/js/87.8e5e2c47.js"><link rel="prefetch" href="/blog/assets/js/88.d2f97880.js"><link rel="prefetch" href="/blog/assets/js/89.a7a762a6.js"><link rel="prefetch" href="/blog/assets/js/9.dde6002e.js"><link rel="prefetch" href="/blog/assets/js/90.c6447971.js"><link rel="prefetch" href="/blog/assets/js/91.403877ec.js"><link rel="prefetch" href="/blog/assets/js/92.44bb8649.js"><link rel="prefetch" href="/blog/assets/js/93.b9f553db.js"><link rel="prefetch" href="/blog/assets/js/94.27628f49.js"><link rel="prefetch" href="/blog/assets/js/95.7ad3dc73.js"><link rel="prefetch" href="/blog/assets/js/96.adc8f3f6.js"><link rel="prefetch" href="/blog/assets/js/97.f5b76103.js"><link rel="prefetch" href="/blog/assets/js/98.e8878d44.js"><link rel="prefetch" href="/blog/assets/js/99.037be397.js">
    <link rel="stylesheet" href="/blog/assets/css/0.styles.5cae76fb.css">
  </head>
  <body class="theme-mode-light">
    <div id="app" data-server-rendered="true"><div class="theme-container sidebar-open have-rightmenu"><header class="navbar blur"><div title="目录" class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/blog/" class="home-link router-link-active"><img src="/blog/img/logo.png" alt="Evan's blog" class="logo"> <span class="site-name can-hide">Evan's blog</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/blog/" class="nav-link">首页</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="前端" class="dropdown-title"><a href="/blog/web/" class="link-title">前端</a> <span class="title" style="display:none;">前端</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>前端文章</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/blog/pages/8143cc480faf9a11/" class="nav-link">JavaScript</a></li></ul></li><li class="dropdown-item"><h4>学习笔记</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/blog/note/javascript/" class="nav-link">《JavaScript教程》</a></li><li class="dropdown-subitem"><a href="/blog/note/js/" class="nav-link">《JavaScript高级程序设计》</a></li><li class="dropdown-subitem"><a href="/blog/note/es6/" class="nav-link">《ES6 教程》</a></li><li class="dropdown-subitem"><a href="/blog/note/vue/" class="nav-link">《Vue》</a></li><li class="dropdown-subitem"><a href="/blog/note/react/" class="nav-link">《React》</a></li><li class="dropdown-subitem"><a href="/blog/note/typescript-axios/" class="nav-link">《TypeScript 从零实现 axios》</a></li><li class="dropdown-subitem"><a href="/blog/note/git/" class="nav-link">《Git》</a></li><li class="dropdown-subitem"><a href="/blog/pages/51afd6/" class="nav-link">TypeScript</a></li><li class="dropdown-subitem"><a href="/blog/pages/4643cd/" class="nav-link">JS设计模式总结</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="页面" class="dropdown-title"><a href="/blog/ui/" class="link-title">页面</a> <span class="title" style="display:none;">页面</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/pages/8309a5b876fc95e3/" class="nav-link">HTML</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/0a83b083bdf257cb/" class="nav-link">CSS</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="技术" class="dropdown-title"><a href="/blog/technology/" class="link-title">技术</a> <span class="title" style="display:none;">技术</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/pages/9a7ee40fc232253e/" class="nav-link">技术文档</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/4c778760be26d8b3/" class="nav-link">GitHub技巧</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/117708e0af7f0bd9/" class="nav-link">Nodejs</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/41f87d890d0a02af/" class="nav-link">博客搭建</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="更多" class="dropdown-title"><a href="/blog/more/" class="link-title">更多</a> <span class="title" style="display:none;">更多</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/pages/f2a556/" class="nav-link">学习</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/aea6571b7a8bae86/" class="nav-link">面试</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/2d615df9a36a98ed/" class="nav-link">心情杂货</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/baaa02/" class="nav-link">实用技巧</a></li><li class="dropdown-item"><!----> <a href="/blog/friends/" class="nav-link">友情链接</a></li></ul></div></div><div class="nav-item"><a href="/blog/about/" class="nav-link">关于</a></div><div class="nav-item"><a href="/blog/pages/beb6c0bd8a66cea6/" class="nav-link">收藏</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/blog/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/blog/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/blog/archives/" class="nav-link">归档</a></li></ul></div></div> <a href="https://github.com/xugaoyi/vuepress-theme-vdoing" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar-hover-trigger"></div> <aside class="sidebar" style="display:none;"><div class="blogger"><img src="https://cdn.jsdelivr.net/gh/xugaoyi/image_store/blog/20200103123203.jpg"> <div class="blogger-info"><h3>Evan Xu</h3> <span>前端界的小学生</span></div></div> <nav class="nav-links"><div class="nav-item"><a href="/blog/" class="nav-link">首页</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="前端" class="dropdown-title"><a href="/blog/web/" class="link-title">前端</a> <span class="title" style="display:none;">前端</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>前端文章</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/blog/pages/8143cc480faf9a11/" class="nav-link">JavaScript</a></li></ul></li><li class="dropdown-item"><h4>学习笔记</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/blog/note/javascript/" class="nav-link">《JavaScript教程》</a></li><li class="dropdown-subitem"><a href="/blog/note/js/" class="nav-link">《JavaScript高级程序设计》</a></li><li class="dropdown-subitem"><a href="/blog/note/es6/" class="nav-link">《ES6 教程》</a></li><li class="dropdown-subitem"><a href="/blog/note/vue/" class="nav-link">《Vue》</a></li><li class="dropdown-subitem"><a href="/blog/note/react/" class="nav-link">《React》</a></li><li class="dropdown-subitem"><a href="/blog/note/typescript-axios/" class="nav-link">《TypeScript 从零实现 axios》</a></li><li class="dropdown-subitem"><a href="/blog/note/git/" class="nav-link">《Git》</a></li><li class="dropdown-subitem"><a href="/blog/pages/51afd6/" class="nav-link">TypeScript</a></li><li class="dropdown-subitem"><a href="/blog/pages/4643cd/" class="nav-link">JS设计模式总结</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="页面" class="dropdown-title"><a href="/blog/ui/" class="link-title">页面</a> <span class="title" style="display:none;">页面</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/pages/8309a5b876fc95e3/" class="nav-link">HTML</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/0a83b083bdf257cb/" class="nav-link">CSS</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="技术" class="dropdown-title"><a href="/blog/technology/" class="link-title">技术</a> <span class="title" style="display:none;">技术</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/pages/9a7ee40fc232253e/" class="nav-link">技术文档</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/4c778760be26d8b3/" class="nav-link">GitHub技巧</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/117708e0af7f0bd9/" class="nav-link">Nodejs</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/41f87d890d0a02af/" class="nav-link">博客搭建</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="更多" class="dropdown-title"><a href="/blog/more/" class="link-title">更多</a> <span class="title" style="display:none;">更多</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/pages/f2a556/" class="nav-link">学习</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/aea6571b7a8bae86/" class="nav-link">面试</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/2d615df9a36a98ed/" class="nav-link">心情杂货</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/baaa02/" class="nav-link">实用技巧</a></li><li class="dropdown-item"><!----> <a href="/blog/friends/" class="nav-link">友情链接</a></li></ul></div></div><div class="nav-item"><a href="/blog/about/" class="nav-link">关于</a></div><div class="nav-item"><a href="/blog/pages/beb6c0bd8a66cea6/" class="nav-link">收藏</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/blog/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/blog/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/blog/archives/" class="nav-link">归档</a></li></ul></div></div> <a href="https://github.com/xugaoyi/vuepress-theme-vdoing" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav>  <ul class="sidebar-links"><li><a href="/blog/pages/f344d070a1031ef7/" class="sidebar-link">ECMAScript 6 简介</a></li><li><a href="/blog/pages/c1edd70a6b7c7872/" class="sidebar-link">let 和 const 命令</a></li><li><a href="/blog/pages/b1ab10a62f7564da/" class="sidebar-link">变量的解构赋值</a></li><li><a href="/blog/pages/ca89eca8adeba5f4/" class="sidebar-link">字符串的扩展</a></li><li><a href="/blog/pages/a650b4a0ebfc9350/" class="sidebar-link">字符串的新增方法</a></li><li><a href="/blog/pages/0473261a6ab0ee8c/" class="sidebar-link">正则的扩展</a></li><li><a href="/blog/pages/5dfea9a0f2d1a392/" class="sidebar-link">数值的扩展</a></li><li><a href="/blog/pages/8ed309d668b20264/" class="sidebar-link">函数的扩展</a></li><li><a href="/blog/pages/e34009d60d8bc4b2/" class="sidebar-link">数组的扩展</a></li><li><a href="/blog/pages/b5e3e0a0ff6e9c25/" class="sidebar-link">对象的扩展</a></li><li><a href="/blog/pages/e85e68947502cf90/" class="sidebar-link">对象的新增方法</a></li><li><a href="/blog/pages/02c86eb2792f3262/" class="sidebar-link">Symbol</a></li><li><a href="/blog/pages/0c21dae358fca16b/" class="sidebar-link">Set 和 Map 数据结构</a></li><li><a href="/blog/pages/f56ec2ab97d60483/" class="sidebar-link">Proxy</a></li><li><a href="/blog/pages/74de3e45e4491e95/" class="sidebar-link">Reflect</a></li><li><a href="/blog/pages/2810ae8985e9bd52/" class="sidebar-link">Promise 对象</a></li><li><a href="/blog/pages/48df907ad3570f3d/" class="sidebar-link">Iterator 和 for-of 循环</a></li><li><a href="/blog/pages/718b48ed9ce0adce/" class="sidebar-link">Generator 函数的语法</a></li><li><a href="/blog/pages/75af7031eb66847b/" class="sidebar-link">Generator 函数的异步应用</a></li><li><a href="/blog/pages/3777253e65bac487/" class="sidebar-link">async 函数</a></li><li><a href="/blog/pages/e831e1593c82bbe0/" class="sidebar-link">Class 的基本语法</a></li><li><a href="/blog/pages/83f8c3a0cd87dd83/" class="sidebar-link">Class 的继承</a></li><li><a href="/blog/pages/efe2fb04eb8ac5fb/" class="sidebar-link">Module 的语法</a></li><li><a href="/blog/pages/a79ca2e64ceae213/" class="sidebar-link">Module 的加载实现</a></li><li><a href="/blog/pages/984bf549204bb266/" class="sidebar-link">编程风格</a></li><li><a href="/blog/pages/32c35f7651d6e58e/" class="sidebar-link">读懂 ECMAScript 规格</a></li><li><a href="/blog/pages/16121351be68691b/" class="sidebar-link">异步遍历器</a></li><li><a href="/blog/pages/a2ba314746bfdbdd/" class="sidebar-link">ArrayBuffer</a></li><li><a href="/blog/pages/7188882b8d65af1b/" aria-current="page" class="active sidebar-link">最新提案</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/blog/pages/7188882b8d65af1b/#do-表达式" class="sidebar-link">do 表达式</a></li><li class="sidebar-sub-header"><a href="/blog/pages/7188882b8d65af1b/#throw-表达式" class="sidebar-link">throw 表达式</a></li><li class="sidebar-sub-header"><a href="/blog/pages/7188882b8d65af1b/#函数的部分执行" class="sidebar-link">函数的部分执行</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/blog/pages/7188882b8d65af1b/#语法" class="sidebar-link">语法</a></li><li class="sidebar-sub-header"><a href="/blog/pages/7188882b8d65af1b/#注意点" class="sidebar-link">注意点</a></li></ul></li><li class="sidebar-sub-header"><a href="/blog/pages/7188882b8d65af1b/#管道运算符" class="sidebar-link">管道运算符</a></li><li class="sidebar-sub-header"><a href="/blog/pages/7188882b8d65af1b/#数值分隔符" class="sidebar-link">数值分隔符</a></li><li class="sidebar-sub-header"><a href="/blog/pages/7188882b8d65af1b/#math-signbit" class="sidebar-link">Math.signbit()</a></li><li class="sidebar-sub-header"><a href="/blog/pages/7188882b8d65af1b/#双冒号运算符" class="sidebar-link">双冒号运算符</a></li><li class="sidebar-sub-header"><a href="/blog/pages/7188882b8d65af1b/#realm-api" class="sidebar-link">Realm API</a></li><li class="sidebar-sub-header"><a href="/blog/pages/7188882b8d65af1b/#命令" class="sidebar-link">#!命令</a></li><li class="sidebar-sub-header"><a href="/blog/pages/7188882b8d65af1b/#import-meta" class="sidebar-link">import.meta</a></li></ul></li><li><a href="/blog/pages/e97bc1e5626b082c/" class="sidebar-link">装饰器</a></li><li><a href="/blog/pages/1cf50330655efc69/" class="sidebar-link">函数式编程</a></li><li><a href="/blog/pages/6a8e2dc558da1b39/" class="sidebar-link">Mixin</a></li><li><a href="/blog/pages/8e8f80f69b775a56/" class="sidebar-link">SIMD</a></li><li><a href="/blog/pages/ea6f3b870f6dab69/" class="sidebar-link">参考链接</a></li></ul> </aside> <div><main class="page"><div class="theme-vdoing-wrapper "><div class="articleInfo-wrap" data-v-0c557b5e><div class="articleInfo" data-v-0c557b5e><ul class="breadcrumbs" data-v-0c557b5e><li data-v-0c557b5e><a href="/blog/" title="首页" class="iconfont icon-home router-link-active" data-v-0c557b5e></a></li> <li data-v-0c557b5e><a href="/blog/note/es6/#《ES6 教程》笔记" data-v-0c557b5e>《ES6 教程》笔记</a></li></ul> <div class="info" data-v-0c557b5e><div title="作者" class="author iconfont icon-touxiang" data-v-0c557b5e><a href="javascript:;" data-v-0c557b5e>阮一峰</a></div> <div title="创建时间" class="date iconfont icon-riqi" data-v-0c557b5e><a href="javascript:;" data-v-0c557b5e>2020-02-09</a></div> <!----></div></div></div> <!----> <div class="content-wrapper"><div class="right-menu-wrapper"><div class="right-menu-margin"><div class="right-menu-content"></div></div></div> <h1><img src="">最新提案<!----></h1>  <div class="theme-vdoing-content content__default"><h1 id="最新提案"><a href="#最新提案" class="header-anchor">#</a> 最新提案</h1> <p>本章介绍一些尚未进入标准、但很有希望的最新提案。</p> <h2 id="do-表达式"><a href="#do-表达式" class="header-anchor">#</a> do 表达式</h2> <p>本质上，块级作用域是一个语句，将多个操作封装在一起，没有返回值。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token punctuation">{</span>
  <span class="token keyword">let</span> t <span class="token operator">=</span> <span class="token function">f</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  t <span class="token operator">=</span> t <span class="token operator">*</span> t <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div> <p>上面代码中，块级作用域将两个语句封装在一起。但是，在块级作用域以外，没有办法得到<code>t</code>的值，因为块级作用域不返回值，除非<code>t</code>是全局变量。</p> <p>现在有一个<a href="https://github.com/tc39/proposal-do-expressions" target="_blank" rel="noopener noreferrer">提案<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>，使得块级作用域可以变为表达式，也就是说可以返回值，办法就是在块级作用域之前加上<code>do</code>，使它变为<code>do</code>表达式，然后就会返回内部最后执行的表达式的值。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">let</span> x <span class="token operator">=</span> <span class="token keyword">do</span> <span class="token punctuation">{</span>
  <span class="token keyword">let</span> t <span class="token operator">=</span> <span class="token function">f</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  t <span class="token operator">*</span> t <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>上面代码中，变量<code>x</code>会得到整个块级作用域的返回值（<code>t * t + 1</code>）。</p> <p><code>do</code>表达式的逻辑非常简单：封装的是什么，就会返回什么。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">// 等同于 &lt;表达式&gt;</span>
<span class="token keyword">do</span> <span class="token punctuation">{</span> <span class="token operator">&lt;</span>表达式<span class="token operator">&gt;</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>

<span class="token comment">// 等同于 &lt;语句&gt;</span>
<span class="token keyword">do</span> <span class="token punctuation">{</span> <span class="token operator">&lt;</span>语句<span class="token operator">&gt;</span> <span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p><code>do</code>表达式的好处是可以封装多个语句，让程序更加模块化，就像乐高积木那样一块块拼装起来。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">let</span> x <span class="token operator">=</span> <span class="token keyword">do</span> <span class="token punctuation">{</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">foo</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token function">f</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">}</span>
  <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">bar</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token function">g</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">}</span>
  <span class="token keyword">else</span> <span class="token punctuation">{</span> <span class="token function">h</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>上面代码的本质，就是根据函数<code>foo</code>的执行结果，调用不同的函数，将返回结果赋给变量<code>x</code>。使用<code>do</code>表达式，就将这个操作的意图表达得非常简洁清晰。而且，<code>do</code>块级作用域提供了单独的作用域，内部操作可以与全局作用域隔绝。</p> <p>值得一提的是，<code>do</code>表达式在 JSX 语法中非常好用。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">return</span> <span class="token punctuation">(</span>
  <span class="token operator">&lt;</span>nav<span class="token operator">&gt;</span>
    <span class="token operator">&lt;</span>Home <span class="token operator">/</span><span class="token operator">&gt;</span>
    <span class="token punctuation">{</span>
      <span class="token keyword">do</span> <span class="token punctuation">{</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>loggedIn<span class="token punctuation">)</span> <span class="token punctuation">{</span>
          <span class="token operator">&lt;</span>LogoutButton <span class="token operator">/</span><span class="token operator">&gt;</span>
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
          <span class="token operator">&lt;</span>LoginButton <span class="token operator">/</span><span class="token operator">&gt;</span>
        <span class="token punctuation">}</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
  <span class="token operator">&lt;</span><span class="token operator">/</span>nav<span class="token operator">&gt;</span>
<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><p>上面代码中，如果不用<code>do</code>表达式，就只能用三元判断运算符（<code>?:</code>）。那样的话，一旦判断逻辑复杂，代码就会变得很不易读。</p> <h2 id="throw-表达式"><a href="#throw-表达式" class="header-anchor">#</a> throw 表达式</h2> <p>JavaScript 语法规定<code>throw</code>是一个命令，用来抛出错误，不能用于表达式之中。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">// 报错</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">Error</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码中，<code>console.log</code>的参数必须是一个表达式，如果是一个<code>throw</code>语句就会报错。</p> <p>现在有一个<a href="https://github.com/tc39/proposal-throw-expressions" target="_blank" rel="noopener noreferrer">提案<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>，允许<code>throw</code>用于表达式。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">// 参数的默认值</span>
<span class="token keyword">function</span> <span class="token function">save</span><span class="token punctuation">(</span>filename <span class="token operator">=</span> <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">TypeError</span><span class="token punctuation">(</span><span class="token string">&quot;Argument required&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token punctuation">}</span>

<span class="token comment">// 箭头函数的返回值</span>
<span class="token function">lint</span><span class="token punctuation">(</span>ast<span class="token punctuation">,</span> <span class="token punctuation">{</span>
  <span class="token function-variable function">with</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">Error</span><span class="token punctuation">(</span><span class="token string">&quot;avoid using 'with' statements.&quot;</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 条件表达式</span>
<span class="token keyword">function</span> <span class="token function">getEncoder</span><span class="token punctuation">(</span><span class="token parameter">encoding</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">const</span> encoder <span class="token operator">=</span> encoding <span class="token operator">===</span> <span class="token string">&quot;utf8&quot;</span> <span class="token operator">?</span>
    <span class="token keyword">new</span> <span class="token class-name">UTF8Encoder</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">:</span>
    encoding <span class="token operator">===</span> <span class="token string">&quot;utf16le&quot;</span> <span class="token operator">?</span>
      <span class="token keyword">new</span> <span class="token class-name">UTF16Encoder</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span> <span class="token operator">:</span>
      encoding <span class="token operator">===</span> <span class="token string">&quot;utf16be&quot;</span> <span class="token operator">?</span>
        <span class="token keyword">new</span> <span class="token class-name">UTF16Encoder</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span> <span class="token operator">:</span>
        <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">Error</span><span class="token punctuation">(</span><span class="token string">&quot;Unsupported encoding&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">// 逻辑表达式</span>
<span class="token keyword">class</span> <span class="token class-name">Product</span> <span class="token punctuation">{</span>
  <span class="token keyword">get</span> <span class="token function">id</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token keyword">this</span><span class="token punctuation">.</span>_id<span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
  <span class="token keyword">set</span> <span class="token function">id</span><span class="token punctuation">(</span><span class="token parameter">value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>_id <span class="token operator">=</span> value <span class="token operator">||</span> <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">Error</span><span class="token punctuation">(</span><span class="token string">&quot;Invalid value&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br></div></div><p>上面代码中，<code>throw</code>都出现在表达式里面。</p> <p>语法上，<code>throw</code>表达式里面的<code>throw</code>不再是一个命令，而是一个运算符。为了避免与<code>throw</code>命令混淆，规定<code>throw</code>出现在行首，一律解释为<code>throw</code>语句，而不是<code>throw</code>表达式。</p> <h2 id="函数的部分执行"><a href="#函数的部分执行" class="header-anchor">#</a> 函数的部分执行</h2> <h3 id="语法"><a href="#语法" class="header-anchor">#</a> 语法</h3> <p>多参数的函数有时需要绑定其中的一个或多个参数，然后返回一个新函数。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">function</span> <span class="token function">add</span><span class="token punctuation">(</span><span class="token parameter">x<span class="token punctuation">,</span> y</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> x <span class="token operator">+</span> y<span class="token punctuation">;</span> <span class="token punctuation">}</span>
<span class="token keyword">function</span> <span class="token function">add7</span><span class="token punctuation">(</span><span class="token parameter">x</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> x <span class="token operator">+</span> <span class="token number">7</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码中，<code>add7</code>函数其实是<code>add</code>函数的一个特殊版本，通过将一个参数绑定为<code>7</code>，就可以从<code>add</code>得到<code>add7</code>。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">// bind 方法</span>
<span class="token keyword">const</span> add7 <span class="token operator">=</span> <span class="token function">add</span><span class="token punctuation">.</span><span class="token function">bind</span><span class="token punctuation">(</span><span class="token keyword">null</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 箭头函数</span>
<span class="token keyword">const</span> <span class="token function-variable function">add7</span> <span class="token operator">=</span> <span class="token parameter">x</span> <span class="token operator">=&gt;</span> <span class="token function">add</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>上面两种写法都有些冗余。其中，<code>bind</code>方法的局限更加明显，它必须提供<code>this</code>，并且只能从前到后一个个绑定参数，无法只绑定非头部的参数。</p> <p>现在有一个<a href="https://github.com/tc39/proposal-partial-application" target="_blank" rel="noopener noreferrer">提案<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>，使得绑定参数并返回一个新函数更加容易。这叫做函数的部分执行（partial application）。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> <span class="token function-variable function">add</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">x<span class="token punctuation">,</span> y</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> x <span class="token operator">+</span> y<span class="token punctuation">;</span>
<span class="token keyword">const</span> addOne <span class="token operator">=</span> <span class="token function">add</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token operator">?</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">const</span> maxGreaterThanZero <span class="token operator">=</span> Math<span class="token punctuation">.</span><span class="token function">max</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token operator">...</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>根据新提案，<code>?</code>是单个参数的占位符，<code>...</code>是多个参数的占位符。以下的形式都属于函数的部分执行。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token function">f</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span> <span class="token operator">?</span><span class="token punctuation">)</span>
<span class="token function">f</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span> <span class="token operator">...</span><span class="token punctuation">)</span>
<span class="token function">f</span><span class="token punctuation">(</span><span class="token operator">?</span><span class="token punctuation">,</span> x<span class="token punctuation">)</span>
<span class="token function">f</span><span class="token punctuation">(</span><span class="token operator">...</span><span class="token punctuation">,</span> x<span class="token punctuation">)</span>
<span class="token function">f</span><span class="token punctuation">(</span><span class="token operator">?</span><span class="token punctuation">,</span> x<span class="token punctuation">,</span> <span class="token operator">?</span><span class="token punctuation">)</span>
<span class="token function">f</span><span class="token punctuation">(</span><span class="token operator">...</span><span class="token punctuation">,</span> x<span class="token punctuation">,</span> <span class="token operator">...</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p><code>?</code>和<code>...</code>只能出现在函数的调用之中，并且会返回一个新函数。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> g <span class="token operator">=</span> <span class="token function">f</span><span class="token punctuation">(</span><span class="token operator">?</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token operator">...</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 等同于</span>
<span class="token keyword">const</span> <span class="token function-variable function">g</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">x<span class="token punctuation">,</span> <span class="token operator">...</span>y</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token function">f</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token operator">...</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>函数的部分执行，也可以用于对象的方法。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">let</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span>
  <span class="token function">f</span><span class="token punctuation">(</span><span class="token parameter">x<span class="token punctuation">,</span> y</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> x <span class="token operator">+</span> y<span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token keyword">const</span> g <span class="token operator">=</span> obj<span class="token punctuation">.</span><span class="token function">f</span><span class="token punctuation">(</span><span class="token operator">?</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">g</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token comment">// 4</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><h3 id="注意点"><a href="#注意点" class="header-anchor">#</a> 注意点</h3> <p>函数的部分执行有一些特别注意的地方。</p> <p>（1）函数的部分执行是基于原函数的。如果原函数发生变化，部分执行生成的新函数也会立即反映这种变化。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">let</span> <span class="token function-variable function">f</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">x<span class="token punctuation">,</span> y</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> x <span class="token operator">+</span> y<span class="token punctuation">;</span>

<span class="token keyword">const</span> g <span class="token operator">=</span> <span class="token function">f</span><span class="token punctuation">(</span><span class="token operator">?</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">g</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 4</span>

<span class="token comment">// 替换函数 f</span>
<span class="token function-variable function">f</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">x<span class="token punctuation">,</span> y</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> x <span class="token operator">*</span> y<span class="token punctuation">;</span>

<span class="token function">g</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 3</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>上面代码中，定义了函数的部分执行以后，更换原函数会立即影响到新函数。</p> <p>（2）如果预先提供的那个值是一个表达式，那么这个表达式并不会在定义时求值，而是在每次调用时求值。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">let</span> a <span class="token operator">=</span> <span class="token number">3</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> <span class="token function-variable function">f</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">x<span class="token punctuation">,</span> y</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> x <span class="token operator">+</span> y<span class="token punctuation">;</span>

<span class="token keyword">const</span> g <span class="token operator">=</span> <span class="token function">f</span><span class="token punctuation">(</span><span class="token operator">?</span><span class="token punctuation">,</span> a<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">g</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 4</span>

<span class="token comment">// 改变 a 的值</span>
a <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">;</span>
<span class="token function">g</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 11</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>上面代码中，预先提供的参数是变量<code>a</code>，那么每次调用函数<code>g</code>的时候，才会对<code>a</code>进行求值。</p> <p>（3）如果新函数的参数多于占位符的数量，那么多余的参数将被忽略。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> <span class="token function-variable function">f</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">x<span class="token punctuation">,</span> <span class="token operator">...</span>y</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">[</span>x<span class="token punctuation">,</span> <span class="token operator">...</span>y<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> g <span class="token operator">=</span> <span class="token function">f</span><span class="token punctuation">(</span><span class="token operator">?</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">g</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// [2, 1]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>上面代码中，函数<code>g</code>只有一个占位符，也就意味着它只能接受一个参数，多余的参数都会被忽略。</p> <p>写成下面这样，多余的参数就没有问题。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> <span class="token function-variable function">f</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">x<span class="token punctuation">,</span> <span class="token operator">...</span>y</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">[</span>x<span class="token punctuation">,</span> <span class="token operator">...</span>y<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> g <span class="token operator">=</span> <span class="token function">f</span><span class="token punctuation">(</span><span class="token operator">?</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token operator">...</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">g</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// [2, 1, 3, 4];</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>（4）<code>...</code>只会被采集一次，如果函数的部分执行使用了多个<code>...</code>，那么每个<code>...</code>的值都将相同。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> <span class="token function-variable function">f</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter"><span class="token operator">...</span>x</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> x<span class="token punctuation">;</span>
<span class="token keyword">const</span> g <span class="token operator">=</span> <span class="token function">f</span><span class="token punctuation">(</span><span class="token operator">...</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token operator">...</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">g</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// [1, 2, 3, 9, 1, 2, 3]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>上面代码中，<code>g</code>定义了两个<code>...</code>占位符，真正执行的时候，它们的值是一样的。</p> <h2 id="管道运算符"><a href="#管道运算符" class="header-anchor">#</a> 管道运算符</h2> <p>Unix 操作系统有一个管道机制（pipeline），可以把前一个操作的值传给后一个操作。这个机制非常有用，使得简单的操作可以组合成为复杂的操作。许多语言都有管道的实现，现在有一个<a href="https://github.com/tc39/proposal-pipeline-operator" target="_blank" rel="noopener noreferrer">提案<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>，让 JavaScript 也拥有管道机制。</p> <p>JavaScript 的管道是一个运算符，写作<code>|&gt;</code>。它的左边是一个表达式，右边是一个函数。管道运算符把左边表达式的值，传入右边的函数进行求值。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code>x <span class="token operator">|</span><span class="token operator">&gt;</span> f
<span class="token comment">// 等同于</span>
<span class="token function">f</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>管道运算符最大的好处，就是可以把嵌套的函数，写成从左到右的链式表达式。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">function</span> <span class="token function">doubleSay</span> <span class="token punctuation">(</span><span class="token parameter">str</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> str <span class="token operator">+</span> <span class="token string">&quot;, &quot;</span> <span class="token operator">+</span> str<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">function</span> <span class="token function">capitalize</span> <span class="token punctuation">(</span><span class="token parameter">str</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> str<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">toUpperCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> str<span class="token punctuation">.</span><span class="token function">substring</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">function</span> <span class="token function">exclaim</span> <span class="token punctuation">(</span><span class="token parameter">str</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> str <span class="token operator">+</span> <span class="token string">'!'</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p>上面是三个简单的函数。如果要嵌套执行，传统的写法和管道的写法分别如下。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">// 传统的写法</span>
<span class="token function">exclaim</span><span class="token punctuation">(</span><span class="token function">capitalize</span><span class="token punctuation">(</span><span class="token function">doubleSay</span><span class="token punctuation">(</span><span class="token string">'hello'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment">// &quot;Hello, hello!&quot;</span>

<span class="token comment">// 管道的写法</span>
<span class="token string">'hello'</span>
  <span class="token operator">|</span><span class="token operator">&gt;</span> doubleSay
  <span class="token operator">|</span><span class="token operator">&gt;</span> capitalize
  <span class="token operator">|</span><span class="token operator">&gt;</span> exclaim
<span class="token comment">// &quot;Hello, hello!&quot;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p>管道运算符只能传递一个值，这意味着它右边的函数必须是一个单参数函数。如果是多参数函数，就必须进行柯里化，改成单参数的版本。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">function</span> <span class="token function">double</span> <span class="token punctuation">(</span><span class="token parameter">x</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> x <span class="token operator">+</span> x<span class="token punctuation">;</span> <span class="token punctuation">}</span>
<span class="token keyword">function</span> <span class="token function">add</span> <span class="token punctuation">(</span><span class="token parameter">x<span class="token punctuation">,</span> y</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> x <span class="token operator">+</span> y<span class="token punctuation">;</span> <span class="token punctuation">}</span>

<span class="token keyword">let</span> person <span class="token operator">=</span> <span class="token punctuation">{</span> <span class="token literal-property property">score</span><span class="token operator">:</span> <span class="token number">25</span> <span class="token punctuation">}</span><span class="token punctuation">;</span>
person<span class="token punctuation">.</span>score
  <span class="token operator">|</span><span class="token operator">&gt;</span> double
  <span class="token operator">|</span><span class="token operator">&gt;</span> <span class="token punctuation">(</span><span class="token parameter">_</span> <span class="token operator">=&gt;</span> <span class="token function">add</span><span class="token punctuation">(</span><span class="token number">7</span><span class="token punctuation">,</span> _<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment">// 57</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>上面代码中，<code>add</code>函数需要两个参数。但是，管道运算符只能传入一个值，因此需要事先提供另一个参数，并将其改成单参数的箭头函数<code>_ =&gt; add(7, _)</code>。这个函数里面的下划线并没有特别的含义，可以用其他符号代替，使用下划线只是因为，它能够形象地表示这里是占位符。</p> <p>管道运算符对于<code>await</code>函数也适用。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code>x <span class="token operator">|</span><span class="token operator">&gt;</span> <span class="token keyword">await</span> f
<span class="token comment">// 等同于</span>
<span class="token keyword">await</span> <span class="token function">f</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span>

<span class="token keyword">const</span> userAge <span class="token operator">=</span> userId <span class="token operator">|</span><span class="token operator">&gt;</span> <span class="token keyword">await</span> fetchUserById <span class="token operator">|</span><span class="token operator">&gt;</span> getAgeFromUser<span class="token punctuation">;</span>
<span class="token comment">// 等同于</span>
<span class="token keyword">const</span> userAge <span class="token operator">=</span> <span class="token function">getAgeFromUser</span><span class="token punctuation">(</span><span class="token keyword">await</span> <span class="token function">fetchUserById</span><span class="token punctuation">(</span>userId<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h2 id="数值分隔符"><a href="#数值分隔符" class="header-anchor">#</a> 数值分隔符</h2> <p>欧美语言中，较长的数值允许每三位添加一个分隔符（通常是一个逗号），增加数值的可读性。比如，<code>1000</code>可以写作<code>1,000</code>。</p> <p>现在有一个<a href="https://github.com/tc39/proposal-numeric-separator" target="_blank" rel="noopener noreferrer">提案<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>，允许 JavaScript 的数值使用下划线（<code>_</code>）作为分隔符。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">let</span> budget <span class="token operator">=</span> <span class="token number">1_000_000_000_000</span><span class="token punctuation">;</span>
budget <span class="token operator">===</span> <span class="token number">10</span> <span class="token operator">**</span> <span class="token number">12</span> <span class="token comment">// true</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>JavaScript 的数值分隔符没有指定间隔的位数，也就是说，可以每三位添加一个分隔符，也可以每一位、每两位、每四位添加一个。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token number">123_00</span> <span class="token operator">===</span> <span class="token number">12_300</span> <span class="token comment">// true</span>

<span class="token number">12345_00</span> <span class="token operator">===</span> <span class="token number">123_4500</span> <span class="token comment">// true</span>
<span class="token number">12345_00</span> <span class="token operator">===</span> <span class="token number">1_234_500</span> <span class="token comment">// true</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>小数和科学计数法也可以使用数值分隔符。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">// 小数</span>
<span class="token number">0.000_001</span>
<span class="token comment">// 科学计数法</span>
<span class="token number">1e10_000</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>数值分隔符有几个使用注意点。</p> <ul><li>不能在数值的最前面（leading）或最后面（trailing）。</li> <li>不能两个或两个以上的分隔符连在一起。</li> <li>小数点的前后不能有分隔符。</li> <li>科学计数法里面，表示指数的<code>e</code>或<code>E</code>前后不能有分隔符。</li></ul> <p>下面的写法都会报错。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">// 全部报错</span>
3_<span class="token punctuation">.</span><span class="token number">141</span>
<span class="token number">3</span><span class="token punctuation">.</span>_141
1_e12
1e_12
123__456
_1464301
1464301_
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>除了十进制，其他进制的数值也可以使用分隔符。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">// 二进制</span>
<span class="token number">0b1010_0001_1000_0101</span>
<span class="token comment">// 十六进制</span>
<span class="token number">0xA0_B0_C0</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>注意，分隔符不能紧跟着进制的前缀<code>0b</code>、<code>0B</code>、<code>0o</code>、<code>0O</code>、<code>0x</code>、<code>0X</code>。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">// 报错</span>
0_b111111000
0b_111111000
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>下面三个将字符串转成数值的函数，不支持数值分隔符。主要原因是提案的设计者认为，数值分隔符主要是为了编码时书写数值的方便，而不是为了处理外部输入的数据。</p> <ul><li>Number()</li> <li>parseInt()</li> <li>parseFloat()</li></ul> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token function">Number</span><span class="token punctuation">(</span><span class="token string">'123_456'</span><span class="token punctuation">)</span> <span class="token comment">// NaN</span>
<span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">'123_456'</span><span class="token punctuation">)</span> <span class="token comment">// 123</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h2 id="math-signbit"><a href="#math-signbit" class="header-anchor">#</a> Math.signbit()</h2> <p><code>Math.sign()</code>用来判断一个值的正负，但是如果参数是<code>-0</code>，它会返回<code>-0</code>。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code>Math<span class="token punctuation">.</span><span class="token function">sign</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">0</span><span class="token punctuation">)</span> <span class="token comment">// -0</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>这导致对于判断符号位的正负，<code>Math.sign()</code>不是很有用。JavaScript 内部使用 64 位浮点数（国际标准 IEEE 754）表示数值，IEEE 754 规定第一位是符号位，<code>0</code>表示正数，<code>1</code>表示负数。所以会有两种零，<code>+0</code>是符号位为<code>0</code>时的零值，<code>-0</code>是符号位为<code>1</code>时的零值。实际编程中，判断一个值是<code>+0</code>还是<code>-0</code>非常麻烦，因为它们是相等的。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token operator">+</span><span class="token number">0</span> <span class="token operator">===</span> <span class="token operator">-</span><span class="token number">0</span> <span class="token comment">// true</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>目前，有一个<a href="http://jfbastien.github.io/papers/Math.signbit.html" target="_blank" rel="noopener noreferrer">提案<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>，引入了<code>Math.signbit()</code>方法判断一个数的符号位是否设置了。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code>Math<span class="token punctuation">.</span><span class="token function">signbit</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token comment">//false</span>
Math<span class="token punctuation">.</span><span class="token function">signbit</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token comment">//true</span>
Math<span class="token punctuation">.</span><span class="token function">signbit</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span> <span class="token comment">//false</span>
Math<span class="token punctuation">.</span><span class="token function">signbit</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">0</span><span class="token punctuation">)</span> <span class="token comment">//true</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>可以看到，该方法正确返回了<code>-0</code>的符号位是设置了的。</p> <p>该方法的算法如下。</p> <ul><li>如果参数是<code>NaN</code>，返回<code>false</code></li> <li>如果参数是<code>-0</code>，返回<code>true</code></li> <li>如果参数是负值，返回<code>true</code></li> <li>其他情况返回<code>false</code></li></ul> <h2 id="双冒号运算符"><a href="#双冒号运算符" class="header-anchor">#</a> 双冒号运算符</h2> <p>箭头函数可以绑定<code>this</code>对象，大大减少了显式绑定<code>this</code>对象的写法（<code>call</code>、<code>apply</code>、<code>bind</code>）。但是，箭头函数并不适用于所有场合，所以现在有一个<a href="https://github.com/zenparsing/es-function-bind" target="_blank" rel="noopener noreferrer">提案<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>，提出了“函数绑定”（function bind）运算符，用来取代<code>call</code>、<code>apply</code>、<code>bind</code>调用。</p> <p>函数绑定运算符是并排的两个冒号（<code>::</code>），双冒号左边是一个对象，右边是一个函数。该运算符会自动将左边的对象，作为上下文环境（即<code>this</code>对象），绑定到右边的函数上面。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token literal-property property">foo</span><span class="token operator">:</span><span class="token operator">:</span>bar<span class="token punctuation">;</span>
<span class="token comment">// 等同于</span>
<span class="token function">bar</span><span class="token punctuation">.</span><span class="token function">bind</span><span class="token punctuation">(</span>foo<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token literal-property property">foo</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">bar</span><span class="token punctuation">(</span><span class="token operator">...</span>arguments<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 等同于</span>
<span class="token function">bar</span><span class="token punctuation">.</span><span class="token function">apply</span><span class="token punctuation">(</span>foo<span class="token punctuation">,</span> arguments<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">const</span> hasOwnProperty <span class="token operator">=</span> <span class="token class-name">Object</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span>hasOwnProperty<span class="token punctuation">;</span>
<span class="token keyword">function</span> <span class="token function">hasOwn</span><span class="token punctuation">(</span><span class="token parameter">obj<span class="token punctuation">,</span> key</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token literal-property property">obj</span><span class="token operator">:</span><span class="token operator">:</span><span class="token function">hasOwnProperty</span><span class="token punctuation">(</span>key<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><p>如果双冒号左边为空，右边是一个对象的方法，则等于将该方法绑定在该对象上面。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> method <span class="token operator">=</span> obj<span class="token operator">:</span><span class="token operator">:</span>obj<span class="token punctuation">.</span>foo<span class="token punctuation">;</span>
<span class="token comment">// 等同于</span>
<span class="token keyword">var</span> method <span class="token operator">=</span> <span class="token operator">:</span><span class="token operator">:</span>obj<span class="token punctuation">.</span>foo<span class="token punctuation">;</span>

<span class="token keyword">let</span> log <span class="token operator">=</span> <span class="token operator">:</span><span class="token operator">:</span>console<span class="token punctuation">.</span>log<span class="token punctuation">;</span>
<span class="token comment">// 等同于</span>
<span class="token keyword">var</span> log <span class="token operator">=</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">.</span><span class="token function">bind</span><span class="token punctuation">(</span>console<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>如果双冒号运算符的运算结果，还是一个对象，就可以采用链式写法。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> map<span class="token punctuation">,</span> takeWhile<span class="token punctuation">,</span> forEach <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">&quot;iterlib&quot;</span><span class="token punctuation">;</span>

<span class="token function">getPlayers</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token operator">:</span><span class="token operator">:</span><span class="token function">map</span><span class="token punctuation">(</span><span class="token parameter">x</span> <span class="token operator">=&gt;</span> x<span class="token punctuation">.</span><span class="token function">character</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token operator">:</span><span class="token operator">:</span><span class="token function">takeWhile</span><span class="token punctuation">(</span><span class="token parameter">x</span> <span class="token operator">=&gt;</span> x<span class="token punctuation">.</span>strength <span class="token operator">&gt;</span> <span class="token number">100</span><span class="token punctuation">)</span>
<span class="token operator">:</span><span class="token operator">:</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token parameter">x</span> <span class="token operator">=&gt;</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><h2 id="realm-api"><a href="#realm-api" class="header-anchor">#</a> Realm API</h2> <p><a href="https://github.com/tc39/proposal-realms" target="_blank" rel="noopener noreferrer">Realm API<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> 提供沙箱功能（sandbox），允许隔离代码，防止那些被隔离的代码拿到全局对象。</p> <p>以前，经常使用<code>&lt;iframe&gt;</code>作为沙箱。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> globalOne <span class="token operator">=</span> window<span class="token punctuation">;</span>
<span class="token keyword">let</span> iframe <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">createElement</span><span class="token punctuation">(</span><span class="token string">'iframe'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
document<span class="token punctuation">.</span>body<span class="token punctuation">.</span><span class="token function">appendChild</span><span class="token punctuation">(</span>iframe<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> globalTwo <span class="token operator">=</span> iframe<span class="token punctuation">.</span>contentWindow<span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>上面代码中，<code>&lt;iframe&gt;</code>的全局对象是独立的（<code>iframe.contentWindow</code>）。Realm API 可以取代这个功能。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> globalOne <span class="token operator">=</span> window<span class="token punctuation">;</span>
<span class="token keyword">const</span> globalTwo <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Realm</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>global<span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码中，<code>Realm API</code>单独提供了一个全局对象<code>new Realm().global</code>。</p> <p>Realm API 提供一个<code>Realm()</code>构造函数，用来生成一个 Realm 对象。该对象的<code>global</code>属性指向一个新的顶层对象，这个顶层对象跟原始的顶层对象类似。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> globalOne <span class="token operator">=</span> window<span class="token punctuation">;</span>
<span class="token keyword">const</span> globalTwo <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Realm</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>global<span class="token punctuation">;</span>

globalOne<span class="token punctuation">.</span><span class="token function">evaluate</span><span class="token punctuation">(</span><span class="token string">'1 + 2'</span><span class="token punctuation">)</span> <span class="token comment">// 3</span>
globalTwo<span class="token punctuation">.</span><span class="token function">evaluate</span><span class="token punctuation">(</span><span class="token string">'1 + 2'</span><span class="token punctuation">)</span> <span class="token comment">// 3</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>上面代码中，Realm 生成的顶层对象的<code>evaluate()</code>方法，可以运行代码。</p> <p>下面的代码可以证明，Realm 顶层对象与原始顶层对象是两个对象。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">let</span> a1 <span class="token operator">=</span> globalOne<span class="token punctuation">.</span><span class="token function">evaluate</span><span class="token punctuation">(</span><span class="token string">'[1,2,3]'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">let</span> a2 <span class="token operator">=</span> globalTwo<span class="token punctuation">.</span><span class="token function">evaluate</span><span class="token punctuation">(</span><span class="token string">'[1,2,3]'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
a1<span class="token punctuation">.</span>prototype <span class="token operator">===</span> a2<span class="token punctuation">.</span>prototype<span class="token punctuation">;</span> <span class="token comment">// false</span>
a1 <span class="token keyword">instanceof</span> <span class="token class-name">globalTwo<span class="token punctuation">.</span>Array</span><span class="token punctuation">;</span> <span class="token comment">// false</span>
a2 <span class="token keyword">instanceof</span> <span class="token class-name">globalOne<span class="token punctuation">.</span>Array</span><span class="token punctuation">;</span> <span class="token comment">// false</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>上面代码中，Realm 沙箱里面的数组的原型对象，跟原始环境里面的数组是不一样的。</p> <p>Realm 沙箱里面只能运行 ECMAScript 语法提供的 API，不能运行宿主环境提供的 API。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code>globalTwo<span class="token punctuation">.</span><span class="token function">evaluate</span><span class="token punctuation">(</span><span class="token string">'console.log(1)'</span><span class="token punctuation">)</span>
<span class="token comment">// throw an error: console is undefined</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码中，Realm 沙箱里面没有<code>console</code>对象，导致报错。因为<code>console</code>不是语法标准，是宿主环境提供的。</p> <p>如果要解决这个问题，可以使用下面的代码。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code>globalTwo<span class="token punctuation">.</span>console <span class="token operator">=</span> globalOne<span class="token punctuation">.</span>console<span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><code>Realm()</code>构造函数可以接受一个参数对象，该参数对象的<code>intrinsics</code>属性可以指定 Realm 沙箱继承原始顶层对象的方法。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> r1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Realm</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
r1<span class="token punctuation">.</span>global <span class="token operator">===</span> <span class="token keyword">this</span><span class="token punctuation">;</span>
r1<span class="token punctuation">.</span>global<span class="token punctuation">.</span><span class="token constant">JSON</span> <span class="token operator">===</span> <span class="token constant">JSON</span><span class="token punctuation">;</span> <span class="token comment">// false</span>

<span class="token keyword">const</span> r2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Realm</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token literal-property property">intrinsics</span><span class="token operator">:</span> <span class="token string">'inherit'</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
r2<span class="token punctuation">.</span>global <span class="token operator">===</span> <span class="token keyword">this</span><span class="token punctuation">;</span> <span class="token comment">// false</span>
r2<span class="token punctuation">.</span>global<span class="token punctuation">.</span><span class="token constant">JSON</span> <span class="token operator">===</span> <span class="token constant">JSON</span><span class="token punctuation">;</span> <span class="token comment">// true</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>上面代码中，正常情况下，沙箱的<code>JSON</code>方法不同于原始的<code>JSON</code>对象。但是，<code>Realm()</code>构造函数接受<code>{ intrinsics: 'inherit' }</code>作为参数以后，就会继承原始顶层对象的方法。</p> <p>用户可以自己定义<code>Realm</code>的子类，用来定制自己的沙箱。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">class</span> <span class="token class-name">FakeWindow</span> <span class="token keyword">extends</span> <span class="token class-name">Realm</span> <span class="token punctuation">{</span>
  <span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">super</span><span class="token punctuation">.</span><span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">let</span> global <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>global<span class="token punctuation">;</span>

    global<span class="token punctuation">.</span>document <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FakeDocument</span><span class="token punctuation">(</span><span class="token operator">...</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    global<span class="token punctuation">.</span>alert <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Proxy</span><span class="token punctuation">(</span>fakeAlert<span class="token punctuation">,</span> <span class="token punctuation">{</span> <span class="token operator">...</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// ...</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p>上面代码中，<code>FakeWindow</code>模拟了一个假的顶层对象<code>window</code>。</p> <h2 id="命令"><a href="#命令" class="header-anchor">#</a> <code>#!</code>命令</h2> <p>Unix 的命令行脚本都支持<code>#!</code>命令，又称为 Shebang 或 Hashbang。这个命令放在脚本的第一行，用来指定脚本的执行器。</p> <p>比如 Bash 脚本的第一行。</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token shebang important">#!/bin/sh</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>Python 脚本的第一行。</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#!/usr/bin/env python</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>现在有一个<a href="https://github.com/tc39/proposal-hashbang" target="_blank" rel="noopener noreferrer">提案<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>，为 JavaScript 脚本引入了<code>#!</code>命令，写在脚本文件或者模块文件的第一行。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">// 写在脚本文件第一行</span>
#<span class="token operator">!</span><span class="token operator">/</span>usr<span class="token operator">/</span>bin<span class="token operator">/</span>env node
<span class="token string">'use strict'</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 写在模块文件第一行</span>
#<span class="token operator">!</span><span class="token operator">/</span>usr<span class="token operator">/</span>bin<span class="token operator">/</span>env node
<span class="token keyword">export</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>有了这一行以后，Unix 命令行就可以直接执行脚本。</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token comment"># 以前执行脚本的方式</span>
$ <span class="token function">node</span> hello.js

<span class="token comment"># hashbang 的方式</span>
$ hello.js
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>对于 JavaScript 引擎来说，会把<code>#!</code>理解成注释，忽略掉这一行。</p> <h2 id="import-meta"><a href="#import-meta" class="header-anchor">#</a> import.meta</h2> <p>开发者使用一个模块时，有时需要知道模板本身的一些信息（比如模块的路径）。现在有一个<a href="https://github.com/tc39/proposal-import-meta" target="_blank" rel="noopener noreferrer">提案<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>，为 import 命令添加了一个元属性<code>import.beta</code>，返回当前模块的元信息。</p> <p><code>import.meta</code>只能在模块内部使用，如果在模块外部使用会报错。</p> <p>这个属性返回一个对象，该对象的各种属性就是当前运行的脚本的元信息。具体包含哪些属性，标准没有规定，由各个运行环境自行决定。一般来说，<code>import.meta</code>至少会有下面两个属性。</p> <p><strong>（1）import.meta.url</strong></p> <p><code>import.meta.url</code>返回当前模块的 URL 路径。举例来说，当前模块主文件的路径是<code>https://foo.com/main.js</code>，<code>import.meta.url</code>就返回这个路径。如果模块里面还有一个数据文件<code>data.txt</code>，那么就可以用下面的代码，获取这个数据文件的路径。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">new</span> <span class="token class-name">URL</span><span class="token punctuation">(</span><span class="token string">'data.txt'</span><span class="token punctuation">,</span> <span class="token keyword">import</span><span class="token punctuation">.</span>meta<span class="token punctuation">.</span>url<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>注意，Node.js 环境中，<code>import.meta.url</code>返回的总是本地路径，即是<code>file:URL</code>协议的字符串，比如<code>file:///home/user/foo.js</code>。</p> <p><strong>（2）import.meta.scriptElement</strong></p> <p><code>import.meta.scriptElement</code>是浏览器特有的元属性，返回加载模块的那个<code>&lt;script&gt;</code>元素，相当于<code>document.currentScript</code>属性。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">// HTML 代码为</span>
<span class="token comment">// &lt;script type=&quot;module&quot; src=&quot;my-module.js&quot; data-foo=&quot;abc&quot;&gt;&lt;/script&gt;</span>

<span class="token comment">// my-module.js 内部执行下面的代码</span>
<span class="token keyword">import</span><span class="token punctuation">.</span>meta<span class="token punctuation">.</span>scriptElement<span class="token punctuation">.</span>dataset<span class="token punctuation">.</span>foo
<span class="token comment">// &quot;abc&quot;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div></div></div>  <div class="page-edit"><div class="edit-link"><a href="https://github.com/xugaoyi/vuepress-theme-vdoing/edit/master/docs/《ES6 教程》笔记/29.最新提案.md" target="_blank" rel="noopener noreferrer">编辑</a> <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></div> <div class="tags"><a href="/blog/tags/?tag=ES6" title="标签">#ES6</a></div> <div class="last-updated"><span class="prefix">上次更新:</span> <span class="time">2022/01/18, 12:59:48</span></div></div> <div class="page-nav-wapper"><div class="page-nav-centre-wrap"><a href="/blog/pages/a2ba314746bfdbdd/" class="page-nav-centre page-nav-centre-prev"><div class="tooltip">ArrayBuffer</div></a> <a href="/blog/pages/e97bc1e5626b082c/" class="page-nav-centre page-nav-centre-next"><div class="tooltip">装饰器</div></a></div> <div class="page-nav"><p class="inner"><span class="prev">
        ←
        <a href="/blog/pages/a2ba314746bfdbdd/" class="prev">ArrayBuffer</a></span> <span class="next"><a href="/blog/pages/e97bc1e5626b082c/">装饰器</a>→
      </span></p></div></div></div> <div class="article-list"><div class="article-title"><a href="/blog/archives/" class="iconfont icon-bi">最近更新</a></div> <div class="article-wrapper"><dl><dd>01</dd> <dt><a href="/blog/pages/dcebaf/"><div>
            一行代码“黑”掉任意网站
            <span class="title-tag">
              原创
            </span></div></a> <span class="date">11-25</span></dt></dl><dl><dd>02</dd> <dt><a href="/blog/pages/a61298/"><div>
            33个非常实用的JavaScript一行代码
            <!----></div></a> <span class="date">11-02</span></dt></dl><dl><dd>03</dd> <dt><a href="/blog/pages/82baa3/"><div>
            使用State Hook
            <!----></div></a> <span class="date">04-06</span></dt></dl> <dl><dd></dd> <dt><a href="/blog/archives/" class="more">更多文章&gt;</a></dt></dl></div></div></main></div> <div class="footer"><div class="icons"><a href="mailto:894072666@qq.com" title="发邮件" target="_blank" class="iconfont icon-youjian"></a><a href="https://github.com/xugaoyi" title="GitHub" target="_blank" class="iconfont icon-github"></a><a href="https://music.163.com/#/playlist?id=755597173" title="听音乐" target="_blank" class="iconfont icon-erji"></a></div> 
  Theme by
  <a href="https://github.com/xugaoyi/vuepress-theme-vdoing" target="_blank" title="本站主题">Vdoing</a> 
    | Copyright © 2019-2022
    <span>Evan Xu | <a href="https://github.com/xugaoyi/vuepress-theme-vdoing/blob/master/LICENSE" target="_blank">MIT License</a></span></div> <div class="buttons"><div title="返回顶部" class="button blur go-to-top iconfont icon-fanhuidingbu" style="display:none;"></div> <div title="去评论" class="button blur go-to-comment iconfont icon-pinglun" style="display:none;"></div> <div title="主题模式" class="button blur theme-mode-but iconfont icon-zhuti"><ul class="select-box" style="display:none;"><li class="iconfont icon-zidong">
          跟随系统
        </li><li class="iconfont icon-rijianmoshi">
          浅色模式
        </li><li class="iconfont icon-yejianmoshi">
          深色模式
        </li><li class="iconfont icon-yuedu">
          阅读模式
        </li></ul></div></div> <!----> <!----> <!----></div><div class="global-ui"><div></div></div></div>
    <script src="/blog/assets/js/app.aaf1b95a.js" defer></script><script src="/blog/assets/js/2.4e88da26.js" defer></script><script src="/blog/assets/js/112.9ecfc68a.js" defer></script>
  </body>
</html>
